package com.lzq.badminton.controller;

import com.lzq.badminton.Exception.SystemException;
import com.lzq.badminton.dto.UserLoginDto;
import com.lzq.badminton.enums.AppHttpCodeEnum;
import com.lzq.badminton.service.WxLoginService;
import com.lzq.badminton.utils.JwtUtil;
import com.lzq.badminton.vo.DoubleToken;
import com.lzq.badminton.vo.ResponseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class WxLoginController {

    @Autowired
    private WxLoginService wxLoginService;

    @PostMapping("/login")
    @ApiOperation("微信登录")
    public ResponseResult login(@RequestBody UserLoginDto userLoginDto){
        System.out.println("进入登录请求!!");
        if(!StringUtils.hasText(userLoginDto.getName())){
            //提示 必须要传用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return wxLoginService.login(userLoginDto);
    }

    @PostMapping("/refresh")
    public ResponseResult refreshToken(HttpServletRequest request){
        String refreshToken = request.getHeader("refresh-token");
        DoubleToken doubleToken = wxLoginService.refresh(refreshToken);
        return ResponseResult.okResult(doubleToken);

    }

    @GetMapping("/hello")
    public String hello(){
        System.out.println("hello!!!!!!!!!!!!!!!!");
        return "hello";
    }
}
